package com.boot;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.LocalDateTime;

@SpringBootTest
public class test {

    @Test
    public void test() {
        //使用默认参数
        Snowflake snowflake = IdUtil.getSnowflake();

        //数字ID
        long id = snowflake.nextId();
        System.out.println(id);

        //字符串ID
        String idStr = snowflake.nextIdStr();
        System.out.println(idStr);

    }
    
    @Test
    public void test1() throws Exception {
        // 参数1: workerId 工作节点ID (0~31)
        // 参数2: datacenterId 数据中心ID (0~31)
        Snowflake snowflake = IdUtil.getSnowflake(2,2);

        for (int i = 0; i < 10; i++) {
            long id = snowflake.nextId();
            System.out.println(id);
        }

    }
    
    @Test
    public void test2() throws Exception {
        Snowflake snowflake = IdUtil.getSnowflake();
        long id = snowflake.nextId();

        // 解析ID
        System.out.println("原始ID: " + id);
        System.out.println("二进制形式: " + Long.toBinaryString(id));
        System.out.println("生成时间: " + snowflake.getGenerateDateTime(id));
        System.out.println("工作节点ID: " + snowflake.getWorkerId(id));
        System.out.println("数据中心ID: " + snowflake.getDataCenterId(id));
    }
}
